/*package com.fare.taluo.application.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fare.taluo.application.StatisticsTaskApplicaion;
import com.fare.taluo.application.plane.channel.SourceApplication;
import com.fare.taluo.core.domain.crawl.statistics.StatisticsTaskEntity;
import com.fare.taluo.util.DateUtils;
import com.fare.taluo.vo.core.SourceEntityVO;
import com.fare.taluo.vo.core.StatisticsTaskEntityVo;
import com.foreveross.taskservice.common.application.ws.IQueryTaskDetailApplicatioin;
import com.foreveross.taskservice.common.application.ws.IStatisticsTaskInfoApplication;


@Transactional
@Scope("singleton")
@Service("statisticsTaskApplicaion")
public class StatisticsTaskApplicaionImpl implements StatisticsTaskApplicaion{

	//加载WEBSERVICSE
//	@Resource(name ="iQueryTaskDetailApplicatioinService")
//	private IQueryTaskDetailApplicatioin iQueryTaskDetailApplicatioin;
	
//	@Resource(name ="iStatisticsTaskInfoApplicationService")
//	private IStatisticsTaskInfoApplication iStatisticsTaskInfoApplication;
	
	@Resource(name ="sourceApplication")
	private SourceApplication sourceApplication;
	
	@Override
	public List<StatisticsTaskEntityVo> getStatisticsTaskEntityVo(String grab_channel,String startTime,String endTime) {
		try {
			//当前日期的前一天（用来拿mysql数据）
			Date yesterdayDate=org.apache.commons.lang3.time.DateUtils.addDays(new Date(),-1);
			String yesterDayStr=DateUtils.dateToStr(yesterdayDate, "yyyy-MM-dd");
			StatisticsTaskEntity st = new StatisticsTaskEntity();
			*//**
			 * 修改，今天 的数据从webServices拿，历史数据从mysql（存储过程中）取出
			 * 1.从webServices拿
			 * 2.查询所有渠道
			 *//*			
			List<SourceEntityVO> sourceList=sourceApplication.findAllSourceEntity();
			Map<String ,Map<Integer,Long>> mapStr=new HashMap<String, Map<Integer,Long>>();
			for(int i=0;i<sourceList.size();i++){
				SourceEntityVO vo=new SourceEntityVO();
				vo=sourceList.get(i);
				long channelId=vo.getId();
				String channelName=vo.getName();
				Map<Integer,Long> maps =iStatisticsTaskInfoApplication.getStatisticsInfo(channelId);
				mapStr.put(channelId+"-"+channelName, maps);
			}
			
			//3.mysql中拿
			List<StatisticsTaskEntity> listpo = st.getStatisticsTaskEntity(grab_channel,startTime,yesterDayStr);
			List<StatisticsTaskEntityVo> listvo = new ArrayList<StatisticsTaskEntityVo>();
			for(StatisticsTaskEntity  ste : listpo){
				StatisticsTaskEntityVo vo = new StatisticsTaskEntityVo();
				//vo.setFailNum(ste.getFailNum());//失败总数
				vo.setAllbyteLength(ste.getAllbyteLength());
//				System.out.println("ste.get ==== "+ste.getAllbyteLength());
				vo.setGrabchannel(ste.getGrabchannel());
				vo.setGrabdate(ste.getGrabdate());
				vo.setSuccessNum(ste.getSuccessNum());
//				vo.setTotal(ste.getTotal());
				vo.setTotal(countadd(ste.getFailNum(), ste.getSuccessNum()));
				vo.setActualNum(ste.getActualNum());
				vo.setStatus(ste.getStatus());
				vo.setGrabchannelID(ste.getGrabChannelID());
//				System.out.println("total:" + ste.getTotal());
//				System.out.println("waiting:" + ste.getWaiting());
//				System.out.println("starting:" + ste.getStarting());
				vo.setWaiting(ste.getWaiting());
				vo.setStarting(ste.getStarting());
				
				if(ste.getStatus()==-2l){ 			// -2表示      失败
					vo.setSecondFail(ste.getFailNum());
				}else if(ste.getStatus()==-3l){ 	//-3表示     抓取失败  
					vo.setThirdFail(ste.getFailNum());
				}else if(ste.getStatus()==-4l){		//-4表示     解析失败
					vo.setForthFail(ste.getFailNum());
				}else if(ste.getStatus()==-10l){	//-10表示  其他
					vo.setOtherFail(ste.getFailNum());
				}else if(ste.getStatus()==0l){      //  0 表示 等待
					vo.setWaiting(ste.getFailNum());
				}else if(ste.getStatus()==1l){      //　1　启动中
					vo.setStarting(ste.getFailNum());
				}else if(ste.getStatus()==2l){      // 2  执行中
					vo.setWorking(ste.getFailNum());
				}
				listvo.add(vo);
			}
			
			if(mapStr.size()>0){
				listvo.addAll(this.detailWS(mapStr));
			}
			
			return pingjie(listvo);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return null;
	}

	//单独处理webservices models[]	
	public List<StatisticsTaskEntityVo> detailWS(Map<String, Map<Integer, Long>> maps) {
		List<StatisticsTaskEntityVo> listvo = new ArrayList<StatisticsTaskEntityVo>();
		String todayStr=DateUtils.getDateStr("YYYY-MM-dd");
		if(maps.size()>0){
			for(Map.Entry<String, Map<Integer,Long>> mapStr:maps.entrySet()){
				String channelId=mapStr.getKey().split("-")[0];
				String channelName=mapStr.getKey().split("-")[1];
				StatisticsTaskEntityVo vo=new StatisticsTaskEntityVo();
				
				vo.setAllbyteLength(0l);
				vo.setGrabchannelID(Long.parseLong(channelId));
				vo.setGrabchannel(channelName);
				vo.setGrabdate(todayStr);
				Map<Integer, Long> dataNumbers=mapStr.getValue();
				Long total=0l;
				for(Map.Entry<Integer,Long> dataMap:dataNumbers.entrySet()){
					if(dataMap.getKey()==3){
						vo.setSuccessNum(dataMap.getValue());
						total+=dataMap.getValue();
					}
					if(dataMap.getKey()==-2){
						vo.setSecondFail(dataMap.getValue());
						//total+=dataMap.getValue();
					}
					if(dataMap.getKey()==-3){
						vo.setThirdFail(dataMap.getValue());
						total+=dataMap.getValue();
					}
					if(dataMap.getKey()==-4){
						vo.setForthFail(dataMap.getValue());
						total+=dataMap.getValue();
					}
					if(dataMap.getKey()==-10){
						vo.setOtherFail(dataMap.getValue());
						total+=dataMap.getValue();
					}
					if(dataMap.getKey()==0){
						vo.setWaiting(dataMap.getValue());
						total+=dataMap.getValue();
					}
					if(dataMap.getKey()==2){
						vo.setStarting(dataMap.getValue());
						total+=dataMap.getValue();
					}
					if(dataMap.getKey()==1){
						vo.setWorking(dataMap.getValue());
						total+=dataMap.getValue();
					}
					
				}
				vo.setTotal(total>0l?total:0l);//总次数，不把等待和执行中的算进去了
				listvo.add(vo);
			}
			return listvo;
		}
		
		return null;
	}
	
	
	@Override
	public List<StatisticsTaskEntityVo> getStatisticsTaskByCondition(
			String grabchannel, String beginTime, String endTime,
			String seachByType, String SymbolType, Long num) {
		try {
			StatisticsTaskEntity st = new StatisticsTaskEntity();
			List<StatisticsTaskEntity> listpo = st.getStatisticsTaskByCondition(grabchannel, beginTime, endTime, seachByType, SymbolType, num);
			List<StatisticsTaskEntityVo> listvo = new ArrayList<StatisticsTaskEntityVo>();
			for(StatisticsTaskEntity  ste : listpo){
				StatisticsTaskEntityVo vo = new StatisticsTaskEntityVo();
				vo.setFailNum(ste.getFailNum());
				vo.setGrabchannel(ste.getGrabchannel());
				vo.setGrabdate(ste.getGrabdate());
				vo.setSuccessNum(ste.getSuccessNum());
				vo.setTotal(ste.getTotal());
				vo.setActualNum(ste.getActualNum());
				vo.setGrabchannelID(ste.getGrabChannelID());
				if(ste.getStatus()==-2l){ 			// -2表示      失败
					vo.setSecondFail(ste.getFailNum());
				}else if(ste.getStatus()==-3l){ 	//-3表示     抓取失败  
					vo.setThirdFail(ste.getFailNum());
				}else if(ste.getStatus()==-4l){		//-4表示     解析失败
					vo.setForthFail(ste.getFailNum());
				}else if(ste.getStatus()==-10l){	//-10表示  其他
					vo.setOtherFail(ste.getFailNum());
				}else if(ste.getStatus()==0l){      //  0 表示 等待
					vo.setWaiting(ste.getFailNum());
				}else if(ste.getStatus()==1l){      //　1　启动中
					vo.setStarting(ste.getFailNum());
				}else if(ste.getStatus()==2l){      // 2  执行中
					vo.setWorking(ste.getFailNum());
				}
				
				listvo.add(vo);
			}
			return pingjie(listvo);
//			return listvo;
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
	//total
			private Long countadd(Long a,Long b){
				if(a!=null&&b!=null)
					return a+b;
				if(a==null & b==null)//这里用& 和  && 小心点
				    return 0l;
				if(a==null & b!=null)
					return b;
				if(b==null & a!=null)
					return a;
				return 0l;
			}
	private List pingjie(List<StatisticsTaskEntityVo> list){
		Map<String,StatisticsTaskEntityVo> map=new LinkedHashMap<String,StatisticsTaskEntityVo>();
		for (int i = 0; i < list.size(); i++) {
			StatisticsTaskEntityVo old=list.get(i);
//			String key=old.getGrabchannel()+"--"+old.getGrabdate()+"--"+old.getSuccessNum()+"--"+old.getActualNum();//拼装成一个key
			String key=old.getGrabchannel()+"--"+old.getGrabdate();//拼装成一个key
			if(map.get( key )!=null){//判断已经存在同个日期，同个渠道，成功数相同，实际抓取数相同 的数据
				int k=1;
				k++;
				StatisticsTaskEntityVo newVo=map.get(key);//将存在的数据取出来
				  //累加
				  Long total=nullToLong(old.getTotal(),newVo.getTotal());
				  Long secondFail=nullToLong(old.getSecondFail(),newVo.getSecondFail());
				  Long thirdFail=nullToLong(old.getThirdFail(),newVo.getThirdFail());
				  Long forthFail=nullToLong(old.getForthFail(),newVo.getForthFail());
				  Long otherFail=nullToLong(old.getOtherFail(),newVo.getOtherFail());
				  Long waiting=nullToLong(old.getWaiting(),newVo.getWaiting());

				  Long starting=nullToLong(old.getStarting(),newVo.getStarting());

				  Long working=nullToLong(old.getWorking(),newVo.getWorking());

				  newVo.setTotal(total-newVo.getSuccessNum());//total在数据库中多加了一遍成功数
				  newVo.setSecondFail(secondFail);
				  newVo.setThirdFail(thirdFail);
				  newVo.setForthFail(forthFail);
				  newVo.setOtherFail(otherFail);
				  newVo.setWaiting(waiting/k);
				  newVo.setStarting(starting);
				  newVo.setWorking(working);
	              map.put(key,newVo);
	       }else{
	    	   if(old.getWorking() ==null){
					old.setWorking(0l);
				}if(old.getWaiting()==null){
					old.setWaiting(0l);
				}if(old.getStarting()==null){
					old.setStarting(0l);
				}if(old.getTotal()==null){
					old.setTotal(0l);
				}
	    	   //old.setTotal(old.getTotal()-old.getWorking()-old.getWaiting()-old.getStarting());
	    	   map.put(key,old);
	       }
		}

		Object s[] = map.keySet().toArray();
		for(int i = 0; i < map.size(); i++) {
			StatisticsTaskEntityVo stu=map.get(s[i]);
		}
		List voList = new ArrayList();
		Iterator iter = map.entrySet().iterator();  //获得map的Iterator
		while(iter.hasNext()) {
			Entry entry = (Entry)iter.next();
			voList.add(entry.getValue());
		}
		
		//降序抓取日期
		sortClass sort = new sortClass();
		Collections.sort(voList,sort);
		
		return voList;
	}
	
	//判断类型为空，赋值0
	private Long nullToLong(Long a,Long b){
		if(a!=null&&b!=null)
			return a+b;
		if(a==null & b==null)//这里用& 和  && 小心点
		    return 0l;
		if(a==null & b!=null)
			return b;
		if(b==null & a!=null)
			return a;
		return 0l;
	}
	
	//抓取日期排序
	public class sortClass implements Comparator {
		public int compare(Object arg0, Object arg1) {
			   SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			   StatisticsTaskEntityVo user0 = (StatisticsTaskEntityVo) arg0;
			   StatisticsTaskEntityVo user1 = (StatisticsTaskEntityVo) arg1;
			   Date d0=null;
			   Date d1=null;
			   if(user0.getGrabdate()!=null){
			    try {
			     d0=sdf.parse(user0.getGrabdate());
			     d1=sdf.parse(user1.getGrabdate());
			    } catch (Exception e) {
			     e.printStackTrace();
			    }
			   }
			    if(d1 ==null && d0==null){
			     return 0;
			    }
			   if(d1 ==null ){
			    return -1;
			   }
			   if(d0 ==null ){
			    return 1;
			   }
			   return d1.compareTo(d0);
			  }
	}
}
*/